<?php


namespace App\Http\Controllers\Admin\Logs;


use App\Http\Controllers\AdminController;
use App\Models\Logs\LoginLog;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class LoginLogController extends AdminController
{

    public function data(Request $request)
    {
        $model = LoginLog::query()->select(['id', 'user_id', 'message', 'platform', 'browser', 'ip', 'ip_address', 'created_at']);
        if ($ip = $request->get("ip")) {
            $model->where('ip', 'like', $ip.'%');
        }
        if ($keyword = $request->get('keywords')) {
            $keyword = filterStr($keyword);
            $model->whereHas('user', function (Builder $query) use ($keyword) {
                $query->where('username', 'like', $keyword.'%')->orWhere('display_name', 'like', $keyword.'%');
            });
        }
        $list = $model->with('user:id,username,display_name')->orderByDesc('id')->paginate($request->get('limit', 10));
        return $this->succeed([
            'total' => $list->total(),
            'rows' => $list->items(),
        ]);
    }
}
